package com.example.newspublish.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.newspublish.pojo.dto.NewsPageDTO;
import com.example.newspublish.pojo.entity.Comment;
import com.example.newspublish.pojo.entity.News;
import com.example.newspublish.pojo.form.NewsForm;
import com.example.newspublish.service.NewsService;
import com.example.newspublish.util.result.PageResult;
import com.example.newspublish.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "新闻管理")
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    NewsService newsService;

    @Operation(summary = "发布新闻", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/addNew")
    public Result addNew(@RequestBody NewsForm newsForm) {
        return newsService.addNew(newsForm);
    }

    @Operation(summary = "删除新闻",security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/deleteNew/{id}")
    public Result deleteNewById(@PathVariable("id") Long id) {
        return newsService.deleteNewById(id);
    }

    @Operation(summary = "获取所有新闻")
    @GetMapping("/getAllList")
    public Result getNewsList() {
        return newsService.getNewsList();
    }

    @Operation(summary = "根据分类获取新闻")
    @GetMapping("/getListByCategoryId/{id}")
    public Result getNewsListById(@PathVariable("id") Long categoryId) {
        return newsService.getNewsListById(categoryId);
    }

    @Operation(summary = "获取新闻详情")
    @GetMapping("/getDetailById/{id}")
    public Result getNewDetailById(@PathVariable("id") Long id) {
        return newsService.getNewDetailById(id);
    }

    @Operation(summary = "搜索新闻")
    @GetMapping("/searchNews")
    public Result<List<News>> searchNews(String key) {

        List<News> list = newsService.search(key);
        return Result.success("搜索新闻成功", list);
    }

    @Operation(summary = "新闻分页")
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        log.debug("新闻分页查询：{}");
        Page<News> newsByPage = newsService.pageQuery(pageNum,pageSize);

        return Result.success("分页查询成功~",newsByPage);
    }

    @Operation(summary = "根据用户名获取该用户发布新闻")
    @GetMapping("/getNewsByUserName")
    public List<News> getNewsByUserName(String author) {
        return newsService.getNewsByUserName(author);
    }
}
